package cn.wildfire.chat.app.main;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.app.extendsion.classes.GroupMembersByApiRequest;
import cn.wildfire.chat.app.extendsion.ui.SmoothToggleAndSlideAnimationForUI;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.group.BasePickGroupMemberActivity;
import cn.wildfire.chat.kit.group.manage.OnPickUserStatusChangeListener;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;
import cn.wildfirechat.remote.GeneralCallback;

/**
 * Create by HEKE, 2019/10/8 18:48
 * TODO 个人禁言设置界面
 */
public class PersonSilentSetActivity extends BasePickGroupMemberActivity {

    protected boolean onCreate;
    // 总选择列表
    protected List<UIUserInfo> checkList;
    // 上次群组中已设置禁言对象
    protected List<String> beforeSilentList;
    // 当前选中所有对象
    private List<UIUserInfo> currentCheckedGroupSilentMembersList;

    protected GroupInfo myGroupInfo;

    @Override
    protected void onGroupMemberChecked(List<UIUserInfo> checkedUserInfos) {
        this.currentCheckedGroupSilentMembersList = checkedUserInfos;
    }

    @Override
    protected void beforeViews() {
        super.beforeViews();
    }

    @Override
    protected void afterViews() {
        Intent intent = getIntent();
        if (intent != null) {
            myGroupInfo = intent.getParcelableExtra("groupInfo");
        }
        isNotGroupChoose = true;
        forceShowSelectorWithNotGroupSet = true;
        initViewModel();
        onCreate = true;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && onCreate) {
            onCreate = false;
            if (myGroupInfo != null) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            bindListener();
                            new GroupMembersByApiRequest(PersonSilentSetActivity.this, myGroupInfo.target, new GroupMembersByApiRequest.OnGetGroupMembersListener() {
                                @Override
                                public void result(List<GroupMember> groupMemberList) {
                                    if (groupMemberList != null) {
                                        List<UserInfo> userInfoList = new ArrayList<>();
                                        beforeSilentList = new ArrayList<>();
                                        for (GroupMember groupMember : groupMemberList) {
                                            if (!groupMember.memberId.equals(myGroupInfo.owner)) {
                                                UserInfo userInfo = ChatManager.Instance().getUserInfo(groupMember.memberId, false);
                                                userInfoList.add(userInfo);
                                                if (groupMember.silence == 1) {
                                                    beforeSilentList.add(groupMember.memberId);
                                                }
                                            }
                                        }
                                        checkList = UIUserInfo.userInfoToUIUserInfo(userInfoList);
                                        initView();
                                        pickUserViewModel.setUsers(checkList);
                                        pickUserViewModel.setInitialCheckedIds(beforeSilentList);
                                        pickUserViewModel.setUncheckableIds(new ArrayList<>());
                                        if (pickGroupMemberFragment != null) {
                                            pickGroupMemberFragment.showQuickIndexBar(true);
                                        }
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, 200);
            }
        }
    }

    /**
     * modify by HEKE , 2019/10/8 18:50 TODO 设置个人禁言操作
     */
    private void setPersonSilent() {
        if (groupViewModel == null) {
            return;
        }
        try {
            List<String> muteList = new ArrayList<>();
            if (currentCheckedGroupSilentMembersList != null && currentCheckedGroupSilentMembersList.size() > 0) {
                for (UIUserInfo uiUserInfo : currentCheckedGroupSilentMembersList) {
                    muteList.add(uiUserInfo.getUserInfo().uid);
                }
            }
            groupViewModel.muteSingle(myGroupInfo.target, 1, muteList, new GeneralCallback() {

                @Override
                public void onSuccess() {
                    ToastTip.tip(getString(R.string.silent_set_success));
                    finish();
                }

                @Override
                public void onFail(int errorCode) {
                    ToastTip.tip(getString(R.string.silent_set_failure) + "：" + errorCode);
                    finish();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * modify by HEKE , 2019/9/4 18:55 TODO 测试滑动隐藏或显示搜索栏 HEKE_SEARCHLAYOUT_ADDMANAGER
     */
    protected SmoothToggleAndSlideAnimationForUI smoothToggleAndSlideAnimationForUI;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        try {
            if (smoothToggleAndSlideAnimationForUI == null) {
                smoothToggleAndSlideAnimationForUI = new SmoothToggleAndSlideAnimationForUI(this,
                        SmoothToggleAndSlideAnimationForUI.FLAG_GROUP_ADD_REMOVE_MANMAGER);
                smoothToggleAndSlideAnimationForUI.setToggleView(pickGroupMemberFragment.getSearchLayout());
                smoothToggleAndSlideAnimationForUI.setSlideView(pickGroupMemberFragment.getUsersRecyclerView(), 0);
            }
            smoothToggleAndSlideAnimationForUI.onTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.dispatchTouchEvent(ev);
    }

    @SuppressLint("ResourceType")
    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(null, R.string.person_silent_set);
        setSearchLayoutVisibility(false);
        getRightTxtView().setBackgroundResource(R.drawable.commom_green_radiusmuch);
        getRightTxtView().setTextColor(getResources().getColor(R.color.white));
        setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.done_title), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = getRightTxtView().getText().toString();
                if (text.equals(getString(R.string.done_title))) {
                    setPersonSilent();
                } else {
                    // 隐藏搜索内容
                    pickGroupMemberFragment.hideSearchContactFragment();
                }
            }
        });
        pickGroupMemberFragment.setOnPickUserStatusChangeListener(onPickUserStatusChangeListener);
    }

    /**
     * 控制右上角文字变化，搜索显示与取消状态监听
     */
    private OnPickUserStatusChangeListener onPickUserStatusChangeListener = new OnPickUserStatusChangeListener() {
        @Override
        public void showSearchResult(boolean searching) {
            getRightTxtView().setText(searching ? getString(R.string.done_title) : getString(R.string.confirm));
        }
    };

}
